package com.xd.study.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.xd.study.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DGF on 2015/4/22.
 */
public class SpinnerActivity extends Activity {

    private TextView textView;
    private Spinner spinner;
    private SimpleAdapter simpleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner);

        textView = (TextView)this.findViewById(R.id.spinner_text);
        spinner = (Spinner)this.findViewById(R.id.spinner);

//        String[] data = {"beijing", "shanghai", "guangzhou", "shenzhen"};
//        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
//        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//        spinner.setAdapter(arrayAdapter);

        List<Map<String, Object>> data = new ArrayList<>();
        for (int i=0;i<5;i++){
            Map<String,Object> item = new HashMap<>();
            item.put("image", R.drawable.item);
            item.put("text", "城市"+i);
            data.add(item);
        }
        String[] from = {"image", "text"};
        int[] to = {R.id.item_image, R.id.item_text};

        simpleAdapter = new SimpleAdapter(this, data, R.layout.listview_item, from, to);
        spinner.setAdapter(simpleAdapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                textView.setText("你选择的是 " +spinner.getItemAtPosition(position).toString());
                Map<String, Object> item = (Map<String, Object>) simpleAdapter.getItem(position);
                textView.setText("你选择的是 " +item.get("text"));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}
